package com.capinfo.controller;

import com.alibaba.fastjson.JSONObject;
import com.capinfo.base.BaseController;
import com.capinfo.base.CurrentUser;
import com.capinfo.core.annotation.Log;
import com.capinfo.entity.SysDict;
import com.capinfo.entity.SysOffice;
import com.capinfo.service.DictService;
import com.capinfo.service.SysOfficeService;
import com.capinfo.util.JsonUtil;
import com.capinfo.util.ReType;
import com.capinfo.utils.RedisUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author: sunbenjin
 * @date: 2020/2/29
 **/
@Controller
@RequestMapping(value="/office")
public class SysOfficeController extends BaseController {

    @Autowired
    private SysOfficeService sysOfficeService;

    @GetMapping(value = "show")
    @RequiresPermissions("office:show")
    public String show(Model model) {
        return "/system/office/office-list";
    }
    @ApiOperation(value = "/showOfficeList", httpMethod = "GET", notes = "展示机构列表")
    @GetMapping(value = "showOfficeList")
    @ResponseBody
    public ReType showOffice(Model model, SysOffice sysOffice, String page, String limit) {
        if(StringUtils.isEmpty(page)){
            page = "0";
        }
        if(StringUtils.isEmpty(limit)){
            limit = "10";
        }
        return sysOfficeService.show(sysOffice, Integer.valueOf(page), Integer.valueOf(limit));
    }
    @GetMapping(value = "addOffice")
    @RequiresPermissions("office:show")
    public String addOffice(Model model, HttpServletRequest request,SysOffice sysOffice) {
        String pId = request.getParameter("pId");
        if(StringUtils.isNotEmpty(pId)){
            model.addAttribute("addChild","addChild");

            SysOffice parent = sysOfficeService.selectByPrimaryKey(pId);
            sysOffice.setParent(parent);


        }
        model.addAttribute("dict",sysOffice);
        return "/system/office/office-form";
    }
    @GetMapping(value = "showOffice")
    @RequiresPermissions("office:show")
    public String showDict(Model model, HttpServletRequest request) {
       String id = request.getParameter("id");
       SysOffice sysOffice = sysOfficeService.selectByPrimaryKey(id);
       if(sysOffice!=null){
           String pId = sysOffice.getPId();
           if(!StringUtils.isEmpty(pId)){

               SysOffice parent = sysOfficeService.selectByPrimaryKey(pId);
               sysOffice.setParent(parent);
           }
       }
       String detail = request.getParameter("detail");
       model.addAttribute("detail",detail);
        model.addAttribute("office",sysOffice);
        return "/system/office/office-form";
    }

    /**
     * 添加下级机构
     * @param model
     * @param request
     * @return
     */
    @GetMapping(value = "addChild")
    @RequiresPermissions("dict:addChild")
    public String addChild(Model model, HttpServletRequest request) {
        String id = request.getParameter("id");
        SysOffice parent = sysOfficeService.selectByPrimaryKey(id);
//        SysOffice parent = new SysOffice();
//        parent.setParent(sysOffice);
        SysOffice sysOffice = new SysOffice();
        sysOffice.setPId(id);
        sysOffice.setParent(parent);
        String detail = request.getParameter("detail");
        model.addAttribute("detail",detail);

        model.addAttribute("office",sysOffice);
        return "/system/office/office-form";
    }
    @ApiOperation(value="addOffice",httpMethod = "POST",notes="添加或更新部门")
    @PostMapping("addOffice")
    @ResponseBody
    public JsonUtil addDict(SysOffice sysOffice){
        JsonUtil j = new JsonUtil();
        CurrentUser user = (CurrentUser) SecurityUtils.getSubject().getSession().getAttribute("curentUser");
        try {
            if(StringUtils.isEmpty(sysOffice.getId())){
                sysOffice.setId(UUID.randomUUID().toString().replaceAll("-", ""));
               sysOffice.setCreateBy(user.getId());
               sysOffice.setCreateDate(new Date());
                sysOfficeService.insertSelective(sysOffice);
            }else{
                sysOffice.setUpdateBy(user.getId());
                sysOffice.setUpdateDate(new Date());
                sysOfficeService.updateByPrimaryKeySelective(sysOffice);
            }

            j.setMsg("保存成功");
        } catch (Exception e) {
            j.setMsg("保存失败");
            j.setFlag(false);

            e.printStackTrace();
        }
        return j;
    }




    @Log(desc = "删除", type = Log.LOG_TYPE.DEL)
    @ApiOperation(value = "/del", httpMethod = "POST", notes = "删除")
    @PostMapping(value = "/del")
    @ResponseBody
    @RequiresPermissions("dict:del")
    public JsonUtil del(String id) {
      //  SysOffice sysOffice = sysOfficeService.selectByPrimaryKey(id);
        //逻辑删除
        return sysOfficeService.delById(id,true);
    }
    @ApiOperation(value="loadAllOffice",httpMethod = "GET",notes="加载字典列表")
    @GetMapping(value="loadAllOffice")
    @ResponseBody
    public JsonUtil loadAllOffice(SysOffice sysOffice,HttpServletRequest request){
        JsonUtil j = new JsonUtil();

        try {
            List<SysOffice> lists = sysOfficeService.selectListByPage(sysOffice);;
            j.setFlag(Boolean.TRUE);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("data",lists);
            j.setJsonObj(jsonObject);
            System.out.println(j);
            j.setMsg("加载完成");
        } catch (Exception e) {
            j.setFlag(Boolean.FALSE);
            j.setMsg("加载异常");
            e.printStackTrace();
        }
        return j;
    }

    @ApiOperation(value="get",httpMethod = "GET",notes="根据id得到部门实例")
    @GetMapping("get")
    @ResponseBody
    public JsonUtil get(String id){
        JsonUtil j = new JsonUtil();
        try {
            SysOffice sysOffice = sysOfficeService.selectByPrimaryKey(id);
            JSONObject jsonObject = new JSONObject();
            if(sysOffice==null){
                sysOffice = new SysOffice();
                sysOffice.setName("");
            }
            jsonObject.put("data",sysOffice);
            j.setJsonObj(jsonObject);
            j.setMsg("获取部门成功");
        } catch (Exception e) {
            j.setMsg("获取部门失败");
            j.setFlag(false);

            e.printStackTrace();
        }
        return j;
    }
}
